class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int maxn = 0, l = 0, r = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == 1) {
                r++;
            }
            else if (nums[i] == 0) {
                l = r;
                r = 0;
            }
            maxn = max(maxn, l + r);
        }
        if (maxn == nums.size()) return maxn - 1;
        else return maxn;
    }
};